package org.proteored.miapeapi.xml.ge.adapter;

import java.util.List;
import java.util.Set;

import org.proteored.miapeapi.cv.ge.GelMatrixName;
import org.proteored.miapeapi.cv.ge.MatrixDimensionUnit;
import org.proteored.miapeapi.cv.ge.MwRangeType;
import org.proteored.miapeapi.cv.ge.MwRangeUnit;
import org.proteored.miapeapi.cv.ge.PhRangeType;
import org.proteored.miapeapi.interfaces.Adapter;
import org.proteored.miapeapi.interfaces.Buffer;
import org.proteored.miapeapi.interfaces.ge.Agent;
import org.proteored.miapeapi.interfaces.ge.GelMatrix;
import org.proteored.miapeapi.interfaces.ge.SampleApplication;
import org.proteored.miapeapi.xml.ge.autogenerated.GEGelMatrix;
import org.proteored.miapeapi.xml.ge.autogenerated.GESampleApplication;
import org.proteored.miapeapi.xml.ge.autogenerated.MIAPEBufferType;
import org.proteored.miapeapi.xml.ge.autogenerated.MIAPESubstanceType;
import org.proteored.miapeapi.xml.ge.autogenerated.ObjectFactory;
import org.proteored.miapeapi.xml.ge.util.GEControlVocabularyXmlFactory;
import org.proteored.miapeapi.xml.util.MiapeXmlUtil;

public class GelMatrixAdapter implements Adapter<GEGelMatrix> {
	private final ObjectFactory factory;
	private final GEControlVocabularyXmlFactory cvFactory;
	private final GelMatrix gelMatrix;

	public GelMatrixAdapter(GelMatrix matrix, ObjectFactory factory2,
			GEControlVocabularyXmlFactory cvFactory2) {
		this.cvFactory = cvFactory2;
		this.factory = factory2;
		this.gelMatrix = matrix;
	}

	@Override
	public GEGelMatrix adapt() {
		GEGelMatrix gelMatrixXML = factory.createGEGelMatrix();
		gelMatrixXML.setAcrylamide(gelMatrix.getAcry());
		gelMatrixXML.setAcrylamideConcentration(gelMatrix.getAcrylamideConcentration());
		gelMatrixXML.setComposed(gelMatrix.getComposed());
		gelMatrixXML.setCrosslinker(gelMatrix.getBisacry());
		if (gelMatrixXML.getDimensionsX() != null)
			gelMatrixXML.setDimensionsX(cvFactory.createParamUnit(gelMatrix.getDimensionsX()
					.toString(), gelMatrix.getDimensionsUnit(), MatrixDimensionUnit
					.getInstance(cvFactory.getCvManager())));
		if (gelMatrixXML.getDimensionsY() != null)
			gelMatrixXML.setDimensionsY(cvFactory.createParamUnit(gelMatrix.getDimensionsY()
					.toString(), gelMatrix.getDimensionsUnit(), MatrixDimensionUnit
					.getInstance(cvFactory.getCvManager())));
		if (gelMatrixXML.getDimensionsZ() != null)
			gelMatrixXML.setDimensionsZ(cvFactory.createParamUnit(gelMatrix.getDimensionsZ()
					.toString(), gelMatrix.getDimensionsUnit(), MatrixDimensionUnit
					.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setGelManufacture(gelMatrix.getGelManufacture());
		gelMatrixXML.setId(MiapeXmlUtil.IdentifierPrefixes.GELMATRIX.getPrefix()
				+ gelMatrix.getId());

		gelMatrixXML.setMWRangeH(cvFactory.createParamUnit(gelMatrix.getMwRangeH(),
				gelMatrix.getMwRangeUnit(), MwRangeUnit.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setMWRangeL(cvFactory.createParamUnit(gelMatrix.getMwRangeL(),
				gelMatrix.getMwRangeUnit(), MwRangeUnit.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setMWRangeType(cvFactory.createCV(gelMatrix.getMwRangeType(), null,
				MwRangeType.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setName(cvFactory.createCV(gelMatrix.getName(), null,
				GelMatrixName.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setPHRangeH(gelMatrix.getPhRangeH());
		gelMatrixXML.setPHRangeL(gelMatrix.getPhRangeL());
		gelMatrixXML.setPHRangeType(cvFactory.createCV(gelMatrix.getPhRangeType(), null,
				PhRangeType.getInstance(cvFactory.getCvManager())));
		gelMatrixXML.setType(gelMatrix.getType());

		if (gelMatrix.getPolymerizationMatrixBuffers() != null)
			addBuffer(gelMatrixXML.getGEPolymerizationMatrixBuffer(),
					gelMatrix.getPolymerizationMatrixBuffers());
		if (gelMatrix.getAdditionalMatrixBuffers() != null)
			addBuffer(gelMatrixXML.getGEAdditionalMatrixBuffer(),
					gelMatrix.getAdditionalMatrixBuffers());
		if (gelMatrix.getAdditionalMatrixSubstances() != null)
			addSubstance(gelMatrixXML.getGEAdditionalMatrixSubstance(),
					gelMatrix.getAdditionalMatrixSubstances());
		if (gelMatrix.getSampleApplications() != null)
			addSampleApplication(gelMatrixXML.getGESampleApplication(),
					gelMatrix.getSampleApplications());

		return gelMatrixXML;
	}

	private void addSampleApplication(List<GESampleApplication> geSampleApplication,
			Set<SampleApplication> sampleApplications) {
		for (SampleApplication sampleApplication : sampleApplications)
			geSampleApplication.add(new SampleApplicationAdapter(sampleApplication, factory,
					cvFactory).adapt());

	}

	private void addSubstance(List<MIAPESubstanceType> geSubstance, Set<Agent> set) {
		for (Agent substance : set)
			geSubstance.add(new AgentAdapter(substance, factory, cvFactory).adapt());

	}

	private void addBuffer(List<MIAPEBufferType> geBuffer, Set<Buffer> buffers) {
		for (Buffer buffer : buffers)
			geBuffer.add(new BufferAdapter(buffer, factory, cvFactory).adapt());

	}

}
